﻿// -----------------------------------------------------------------------
// <copyright file="Controller.cs" company="">
// TODO: Update copyright text.
// </copyright>
// -----------------------------------------------------------------------

namespace TaleGeneratorInterface
{
	using System;
	using System.Collections.Generic;
	using System.Linq;
	using System.Text;
	using FrameNetViewingControl;
	using TaleGeneratorModel;
	using System.Data.SqlServerCe;
	using CommonStructs;
	using System.Drawing;

	/// <summary>
	/// TODO: Update summary.
	/// </summary>
	public class Controller
	{
		FrameNetDiagram frameNetView;
		string connString;
		SqlCeConnection conn;
		TaleClasses frameNet;


		public FrameType NewFrameType
		{
			get;
			set;
		}

		public Controller(string connStr, FrameNetDiagram view)
		{
			frameNetView = view;
			connString = connStr;
			conn = new SqlCeConnection(connString);
			conn.Open();
			frameNet = new TaleClasses(conn);

			ShowFrameNet();

			AssignViewEventHandlers();
		}

		private void ShowFrameNet()
		{
			frameNetView.Clear();
			foreach (var frame in frameNet.Frame)
			{
				frameNetView.AddFrame(new FrameData { id = frame.Id, location = new Point(frame.X, frame.Y), name = frame.Name });
			}
			foreach (var frame in frameNet.Frame.Where(x => x.Is_a != null))
			{
				frameNetView.LinkAdd(frame.Id, frame.Is_aFrame.Id, LinkType.IsA);
			}
			foreach (var subframeSlot in frameNet.Slot.Where(x => x.SubframeId != null))
			{
				frameNetView.LinkAdd(subframeSlot.Frame, subframeSlot.SubframeId.Value, LinkType.SubFrame);
			}
		}

		private void AssignViewEventHandlers()
		{
			frameNetView.EventFrameCreate += new Action<System.Drawing.Point>(frameNetView_EventFrameCreate);
			frameNetView.EventFrameMove += new Action<int, System.Drawing.Point>(frameNetView_EventFrameMove);
			frameNetView.EventFrameRemove += new Action<int>(frameNetView_EventFrameRemove);
			frameNetView.EventFrameRename += new Action<int, string>(frameNetView_EventFrameRename);
			frameNetView.EventFrameSelect += new Action<int>(frameNetView_EventFrameSelect);
			frameNetView.EventLinkCreate += new Action<int, int, CommonStructs.LinkType>(frameNetView_EventLinkCreate);
			frameNetView.EventLinkRemove += new Action<int, int, CommonStructs.LinkType>(frameNetView_EventLinkRemove);
			frameNetView.EventUnselectAll += new Action(frameNetView_EventUnselectAll);
		}

		void frameNetView_EventUnselectAll()
		{
			throw new NotImplementedException();
		}

		void frameNetView_EventLinkRemove(int from, int to, CommonStructs.LinkType type)
		{
			FrameNetManager mgr = new FrameNetManager(frameNet);
			if (mgr.MayDeleteLink(from, to, type))
			{
				mgr.DeleteLink(from, to, type);
				frameNetView.LinkRemove(from, to);
			}
			else
				System.Windows.Forms.MessageBox.Show("Невозможно удалить дугу");
		}

		void frameNetView_EventLinkCreate(int arg1, int arg2, CommonStructs.LinkType arg3)
		{
			throw new NotImplementedException();
		}

		void frameNetView_EventFrameSelect(int obj)
		{
			throw new NotImplementedException();
		}

		void frameNetView_EventFrameRename(int arg1, string arg2)
		{
			throw new NotImplementedException();
		}

		void frameNetView_EventFrameRemove(int obj)
		{
			throw new NotImplementedException();
		}

		void frameNetView_EventFrameMove(int arg1, System.Drawing.Point arg2)
		{
			throw new NotImplementedException();
		}

		void frameNetView_EventFrameCreate(System.Drawing.Point location)
		{
			Frame newFrame = new Frame();
			FrameNetManager mgr = new FrameNetManager(frameNet);
			if (NewFrameType == FrameType.Object)
			{
				Frame objectFrame = frameNet.Frame.SingleOrDefault(x => x.Name == "Объект");
				if (objectFrame != null)
				{				
					newFrame.Name = "Фрейм " + newFrame.Id;
					frameNet.SubmitChanges();
					mgr.AddLink(newFrame.Id, objectFrame.Id, LinkType.IsA);
				}			
			}
			else 
			{
 
			}
			
		}


	}
}
